﻿using System;
using System.Security.Principal;
using System.Threading;
using System.Web.Security;
using System.Windows.Forms;
using ShippingManage.DataAccessor.Interfaces;
using ShippingManage.Services;

namespace ShippingManage
{
	public partial class frmSignIn : Form
	{
		public frmSignIn()
		{
			InitializeComponent();
		}

		private void SignButton_Click(object sender, EventArgs e)
		{
			string username = UsernameTextbox.Text;
			string password = Dng.Utilities.Cryptographer.MD5(PasswordTextbox.Text);
			if (Membership.ValidateUser(username, password))
			{
				var user = Membership.GetUser(username);
				var identity = new GenericIdentity(user.UserName);
				var principal = new RolePrincipal(identity);
				Thread.CurrentPrincipal = principal;

				//sign success
				if (NetworkHelper.I.CanConnectToServer)
				{
					//compare current user info version with server
					SyncService service = new SyncService();
					service.SynUserInfo((ShippingMemberUser)user);
				}
			}
			else
			{
				MessageBox.Show("Login failed");
			}
		}
	}
}
